var app = getApp();
import {
  api,
  util,
  dialog,
  forms,
  storage
} from '../../../js/util'
import {
  router
} from '../../../js/router';
import {
  advert
} from '../../../js/modules/market.js'
import {
  theme
} from '../../../js/modules/theme.js'
import authorize from '../../../js/modules/authorize.js'
const componentBehavior = require('../../../js/common/ComponentBehavior')
import { throttle } from '../../../js/newUtil';
Component({
  behaviors: [componentBehavior],
  /**
   * 组件的属性列表
   */
  properties: {
    pageKey: {
      type: String,
      value: "index"
    }
  },
  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {

    },
    hide() {}
  },
  /**
   * 组件的初始数据
   */
  data: {
    imgDomain:api.imgDomain,
    item: {},
    advert_key: "advert_key",
    user_key:'0',
    isShow: false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getAdverScreen: throttle(function () {
      var that = this;
      var entity = authorize.getEntity() || authorize.getDefault();
      this.setData({
        user_key:entity.fkId
      });
      var obj = storage.get(that.data.advert_key+'_'+entity.fkId);
      console.log(that.data.advert_key+'_'+entity.fkId,'====obj',obj);
      if ((obj == null || obj == "") || (obj != null && obj.day != new Date().getDate())){
        //重新请求数据
        advert.getAdvertScreen({}, function (content) {
          var isShow = false,
            item = {
              clickNum: 0,//点击次数
              scanNum: 0,//浏览次数
              index: 0,//广告索引
              loopNum:0,//循环次数
              day: new Date().getDate(),//当前日期
              list: []
            };
          if (content && content.length > 0) {
            item.list = content;
          }
          else {
            that.setData({
              isShow: false
            });
            that.onNoticeParent();
            return;
          }

          that.setData({
            isShow: true,
            item: item
          });
          storage.put(that.data.advert_key+'_'+that.data.user_key, item);
          item.scanNum += 1;
          that.updateScanNum(item, 1);
        });
      }
      else if ((obj != null && obj.loopNum>=3) || (obj.index == obj.list.length - 1)){  
        // that.setData({
        //   isShow: false
        // })
        that.onNoticeParent();
        console.log('2222')
      }
      else if (obj.index >= obj.list.length - 1&&obj.loopNum < 3){
        obj.index=0;
        obj.loopNum += 1;
        obj.scanNum = obj.scanNum + 1;      
        storage.put(that.data.advert_key+'_'+that.data.user_key, obj);
        that.setData({
          item: obj,
          isShow: true
        })
        that.updateScanNum(obj, 1);
        console.log('3333')
      }
      else{
        obj.index = obj.index + 1;
        obj.scanNum = obj.scanNum + 1;
        storage.put(that.data.advert_key+'_'+that.data.user_key, obj);
        that.setData({
          item:obj,
          isShow:true
        })
        that.updateScanNum(obj, 1);
        console.log('4444')
      }
    }, 500),
    onNoticeParent(e) {
      this.triggerEvent('completeCallBack', {})
    },
    /**
     * 更新浏览数量
     */
    updateScanNum(item,t){
      var that = this;
      var id = item.id;
      var type = item.type;
      advert.updateScanNum({ id: item.list[item.index % item.list.length].Id, type: t, count: t == 1 ? item.scanNum : item.clickNum }, function () {});
    },
    /**
     * 单击领取
     */
    onClickAdvertPacks(e) {
      if (!authorize.isLogin('store')) { 
        authorize.redirect('store'); 
        return; 
      }
      var that=this;
      var item = this.data.item;
      item.clickNum = item.clickNum+1;
      storage.put(that.data.advert_key+'_'+that.data.user_key, item);
      that.updateScanNum(item,2);
      that.setData({
        item:item,
        isShow:false
      })
      //跳转
      theme.linkTo(item.list[item.index%item.list.length].Link);
    },
    onCloseAdvertPacks(e) {
      let that=this;
      that.setData({
        isShow: false
      },()=>{
        // storage.put(that.data.advert_key+'_'+that.data.user_key, 'null');
        // return;
        if (that.timer!=null){
          clearTimeout(that.timer);
        }
        that.timer=setTimeout(function () {
          that.getAdverScreen();
        }, 1000);
      });
      // storage.put(that.data.advert_key+'_'+that.data.user_key, null);
    }
  }
})